import {defineStore} from 'pinia'
import {useAccountStore} from "./AccountStore.ts";
import type {FavorForum} from "../network/types/Forum.ts";
import {CacheMap, useCache} from "../utils/CacheUtils.ts";

const api = () => useAccountStore().getNgaApi().forum;

export const useForumStore = defineStore('ForumStore', {
    state: () => ({
        cache: new CacheMap<FavorForum[]>(),
        favorForums: [] as FavorForum[],
    }),
    actions: {
        async favorList(force?: boolean) {
            const userId = useAccountStore().getAccountUserId();
            const key = `favor forums ${userId}`
            return useCache(this.cache, key, force || false, () => api().favorList()).then(res => {
                this.favorForums = res;
                return res
            })
        },
        search(key: string, page?: number) {
            return api().search(key, page)
        },
        favorAdd(fid?: number | undefined, stid?: number | undefined) {
            return api().favorAdd(fid, stid)
        },
        favorDel(fid?: number | undefined, stid?: number | undefined) {
            return api().favorDel(fid, stid)
        },
        blockSubForum(forumId: number, mirrorId: number, type: 'add' | 'del') {
            return api().blockSubForum(forumId, mirrorId, type)
        }
    },
})